package com.hyk.exception;


import com.hyk.dto.ApiResult;
import com.hyk.utils.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

//需求：是来改造原来的异常情况下的返回
//被ControllerAdvice修饰的类，可以重新返回结果
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    /*@ExceptionHandler(value = ArithmeticException.class)
    public ApiResult arithmenticException(ArithmeticException e){
        e.printStackTrace();
        return N.no(e.getMessage());
    }*/

    /*@ExceptionHandler(value = ArrayIndexOutOfBoundsException.class)
    public ApiResult arrayIndexOutofBoundsException(ArrayIndexOutOfBoundsException e){
        e.printStackTrace();
        return N.no(e.getMessage());
    }*/

    //拦截所有java自带的异常
//    @ExceptionHandler(value = Exception.class)
//    public ApiResult exception(Exception e){
//        e.printStackTrace();
//        return N.no(e.getMessage());
//    }

    //自己设置的异常拦截
    @ExceptionHandler(value = GlobalException.class)
    public ApiResult globalException(GlobalException e){
        e.printStackTrace();
        return R.no(e.getApiEnum());
    }

}
